%用于霍夫线的检测
%input linepoint
%output n*2数组 点+斜率

% fpspoint = 100;
% look_area = 0.015;
% look_up = 0.02;
% look_down = 0.0005;
% look_times = 30;
% 
% pc = sortrows(points,5,'descend');
% ind = find(pc(:,5),1,'last');
% linepoint = pc(1:ind,:);
%save('linepoint.mat','linepoint');
load('linepoint.mat')
ind = 9873;

%霍夫变换
%将图像二维空间的一个点映射到p=x*cos(theta)+y*sin(theta)方程对应的参数空间的一条曲线
data=zeros(3140,20000);
for n = 1:ind
    if linepoint(n, 5) ==1
        i = linepoint(n, 1)*10000;
        j = linepoint(n, 2)*10000;
        for theta=linspace(0.001, 3.14, 3140)
                data(round(theta*1000),round(i*cos(theta)+j*sin(theta)+))= ...
                data(round(theta*1000),round(i*cos(theta)+j*sin(theta)))+1;
        end      
    end
end

theta=0;
ma=0;
for i=1:3140                %寻找曲线相交最多的那个点，即找最大值
    for j=1:14142
        if data(i,j)>ma
            ma=data(i,j);
            maxi = i;
            maxj = j;
            theta=i/1000;
            %rou=j-m-n;
        end
    end
end
figure;imshow(data)     %形象的显示参数空间曲线
fig = figure;hold on;
draw(linepoint(1:ind,:),'r');
re_k=cos(theta)/sin(theta)  %求得的斜率